CRM 2011 Javascript checking logged-in User’s Team

Hi All,

Here is a more better way to Check logged-in User’s Team

Note: You have to download the CRM Service Toolkit and add that as a web-resource in CRM. Also call this web-resource on the form where you are calling the below Jscript

The JScript Code is:

function checkUserTeam()
{
var UserID = Xrm.Page.context.getUserId();

//FetchXML to retrieve User’s Team
var fetchXML = “<fetch version=’1.0′ output-format=’xml-platform’ mapping=’logical’ distinct=’true’>” +
“<entity name=’team’>” +
“<attribute name=’name’ />” +
“<attribute name=’businessunitid’ />” +
“<attribute name=’teamid’ />” +
“<order attribute=’name’ descending=’false’ />” +
“<link-entity name=’teammembership’ from=’teamid’ to=’teamid’ visible=’false’ intersect=’true’>” +
“<link-entity name=’systemuser’ from=’systemuserid’ to=’systemuserid’ alias=’ab’>” +
“<filter type=’and’>” +
“<condition attribute=’systemuserid’ operator=’eq’ value='”+UserID+”‘ />” +
“</filter>” +
“</link-entity>” +
“</link-entity>” +
“</entity>” +
“</fetch>”;

var fetch_detail = CrmServiceToolkit.Fetch(fetchXML);

//alert(fetch_detail.length);
if (parseInt(fetch_detail.length) > 0)
{
var Team;
for (var i = 0; i < fetch_detail.length; i++)
{
if(Team == null)
{
Team = fetch_detail[i].getValue(‘name’);
}
else //If the User is linked to Multiple Teams
{
Team = Team + “, ” + fetch_detail[i].getValue(‘name’);
}
}
//Checking Team Name(Here RM Assignment is the Team’s Name)
var rslt = Team.indexOf(‘RM Assignment’);
if(rslt != -1)
{
//Here I am enabling the field if User belongs to the Team “RM Assignment”
Xrm.Page.ui.controls.get(“new_rm”).setDisabled(false);
}
else {
Xrm.Page.ui.controls.get(“new_rm”).setDisabled(true);
}
}
}

Cheers,

Naren

Leave a comment